<template>
	<view>
		<searchBar @search="toSearch"></searchBar>

		<!-- 朋友列表 -->
		<friendsListBlock :list="friendList" :isSearchResult="!!searchKey" :selectedIds="selectedIds" :hasSwitch="true" @switch="toggleSwitch">
		</friendsListBlock>

		<!-- 右侧字母表 -->
		<letterList></letterList>
	</view>
</template>

<script>
	import {
		mapState
	} from "vuex"
	export default {
		data() {
			return {
				pageType: 2, // 2：选择谁可见 3：选择谁不可见  4:不让他(她)看 5：不看他(她) 注：1-没用到
				selectedIds: [],
				searchKey:''
			}
		},
		computed: {
			...mapState(['friendList', 'newEvaluate','userInfo']),
		},
		mounted() {
			this.$store.dispatch('getFriendList')
		},
		onLoad(options) {
			this.pageType = options.type * 1 || 2
			let pageTitle = '给谁看'
			switch (this.pageType) {
				case 2:
					pageTitle = '给谁看'
					this.selectedIds = this.newEvaluate.toSeeUsers
					break
				case 3:
					pageTitle = '不给谁看'
					this.selectedIds = this.newEvaluate.toNotSeeUsers
					break
				case 4:
					pageTitle = '不让他(她)看'
					this.getUnseeFrineds()
					break
				case 5:
					pageTitle = '不看他(她)'
					this.getUnlikeFrineds()
					break
			}
			uni.setNavigationBarTitle({
				title: pageTitle
			});
		},
		methods: {
			toSearch(key) {
                this.searchKey = key
			    this.$store.dispatch('getFriendList', key? { keywords: key} : {})
			},
			toggleSwitch(res) {
				this.selectedIds = res || []

				if (this.pageType == 2) {
					this.$store.commit('updateNewEvaluate', {
						toSeeUsers: res
					})
				}

				if (this.pageType == 3) {
					this.$store.commit('updateNewEvaluate', {
						toNotSeeUsers: res
					})
				}

				if (this.pageType == 4) {
					this.setUnseeFrineds(res)

				}
				if (this.pageType == 5) {
					this.setUnlikeFrineds(res)

				}
			},

			formatIds(ids) {
				return ids.map(id => {
					return {
						fromUserId: this.userInfo.id,
						toUserId: id
					}
				})
			},
			getUnseeFrineds(){
				this.$api.getUnseeFrineds().then((res) => {
					// 不让他看的列表
					this.initSelectedIds(res)
				})
			},
			getUnlikeFrineds(){
				this.$api.getUnlikeFrineds().then((res) => {
					// 不看他的列表
					this.initSelectedIds(res)
				})
			},
			initSelectedIds(res){
				if(res && res.length){
					this.selectedIds = res.map((item)=>{
						return item.toUserId
					})
				}
			},
			setUnseeFrineds(ids) {
				ids = this.formatIds(ids)
				this.$api.setUnseeFrineds(ids)
			},
			setUnlikeFrineds(ids) {
				ids = this.formatIds(ids)
				this.$api.setUnlikeFrineds(ids)
			}
		}
	}
</script>


<style lang="less">

</style>
